﻿using System;
using System.ComponentModel;
using System.Security;
using System.Windows;
using System.Windows.Input;
using Co9320.NamaAlert.Extension.Command;
using Co9320.NamaAlert.Extension.Model;

namespace Co9320.NamaAlert.Extension.ViewModel
{
    class OptionViewModel : INotifyPropertyChanged
    {

        private ISetting setting;
        public OptionViewModel(ISetting setting)
        {
            this.setting = setting;
        }

        public event PropertyChangedEventHandler PropertyChanged;
        public void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = this.PropertyChanged;
            if (handler != null)
            {
                var e = new PropertyChangedEventArgs(propertyName);
                handler(this, e);
            }
        }

        #region プロパティ
        /// <summary>
        /// ユーザー名
        /// </summary>
        public string UserName
        {
            get;
            set;
        }

        /// <summary>
        /// パスワード
        /// </summary>
        public SecureString Password
        {
            get;
            set;
        }
        #endregion

        private RelayCommand _testSendCommand;
        bool _testSending = false;
        public ICommand TestSendCommand
        {
            get
            {
                if (_testSendCommand == null)
                {
                    _testSendCommand = new RelayCommand(param =>
                    {
                        if (this.UserName == "" || this.Password.Length == 0)
                        {
                            MessageBox.Show("Twitter投稿に失敗しました。\n必須項目が設定されていません。");
                        }
                        else
                        {
                            _testSending = true;
                            TestSetting testSend = new TestSetting();
                            this.save(testSend);
                            try
                            {
                                new Twitter(testSend).SendUpdate("テスト投稿 by ニコ生アラート", stream =>
                                {
                                    MessageBox.Show("Twitter投稿に成功しました。");
                                });
                            }
                            catch (Exception e)
                            {
                                MessageBox.Show("Twitter送信に失敗しました。\n" + e.Message);
                                _testSending = false;
                            }
                        }
                    }, param => (this.UserName == "" || this.Password.Length == 0) && !this._testSending);
                }
                return _testSendCommand;
            }

        }

        public void Save()
        {
            this.save(this.setting);
        }

        private void save()
        {
            this.save(this.setting);
        }
        private void save(ISetting setting)
        {
            setting.UserName = this.UserName;
            setting.Password = this.Password;
        }
    }
}
